use std::{
    error::Error,
    fmt::{Display, Formatter, Result},
    io,
};

#[derive(Debug)]
pub enum CustomError {
    FileReadError(String),
    SocketReadError(String),
    IoError(io::Error),
    SqlParseError,
    IndexOutOfBounds,
    ExecuteError(String),
    ParseError(String),
    Unknown,
}

impl Error for CustomError {}

impl Display for CustomError {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            CustomError::FileReadError(msg) => write!(f, "{}", msg),
            CustomError::SocketReadError(msg) => write!(f, "{}", msg),
            CustomError::IoError(err) => write!(f, "Io error, {}", err),
            CustomError::SqlParseError => write!(f, "Sql parse error"),
            CustomError::IndexOutOfBounds => write!(f, "Index out of bounds"),
            CustomError::ExecuteError(msg) => write!(f, "Execute {}", msg),
            CustomError::ParseError(msg) => write!(f, "{}", msg),
            CustomError::Unknown => write!(f, "Unknown error"),
        }
    }
}

impl From<io::Error> for CustomError {
    fn from(err: io::Error) -> CustomError {
        CustomError::IoError(err)
    }
}
